package com.itgaoshu.hospitalproject.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itgaoshu.hospitalproject.bean.Area;
import com.itgaoshu.hospitalproject.bean.Drugdictionary;
import com.itgaoshu.hospitalproject.service.DrugdictionaryService;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("drugdictionary")
public class DrugdictionaryController {
    @Autowired
    private DrugdictionaryService drugdictionaryService;

    //查询药品的方法
    @ResponseBody
    @RequestMapping("findAllSdrugdictionary")
    public  Object findAllSdrugdictionary(Integer limit,Integer page,String drugdictionary){
        PageHelper.startPage(page,limit);
        Drugdictionary drugdictionary1 = new Drugdictionary();
        drugdictionary1.setDrugName(drugdictionary);
        List<Drugdictionary> drugdictionaries = drugdictionaryService.findAllSdrugdictionary(drugdictionary1);
        PageInfo pageInfo = new PageInfo(drugdictionaries);
        Map<String,Object> tableData = new HashMap<>();
        tableData.put("code",0);
        tableData.put("msg","");
        tableData.put("count",pageInfo.getTotal());
        tableData.put("data",pageInfo.getList());
        return tableData;
    }

    //添加药品的方法
    @RequestMapping("addSdrugdictionary")
    @ResponseBody
    public Object addSdrugdictionary(Drugdictionary drugdictionary){
        int result = drugdictionaryService.addSdrugdictionary(drugdictionary);
        if(result>0){
            return "添加成功";
        }else{
            return "添加失败";
        }
    }

    //查询所有产地的方法
    @ResponseBody
    @RequestMapping("findAllArea")
    public List<Area> findAllArea(){
        List<Area> areas =drugdictionaryService.findAllArea();
        return areas;
    }

    //查询所有计量的方法
    @ResponseBody
    @RequestMapping("findAllUnit")
    public Object findAllUnit(){
        return drugdictionaryService.findAllUnit();
    }

    //查询所有类型的方法
    @RequestMapping("findAllType")
    @ResponseBody
    public  Object findAllType(){
        return drugdictionaryService.findAllType();
    }

    //删除药品的方法
    @ResponseBody
    @RequestMapping("deleteSdrugdictionary")
    public Object deleteSdrugdictionary(Integer drugId){
        int result = drugdictionaryService.deleteSdrugdictionary(drugId);
        if(result>0){
            return "删除成功";
        }else{
            return "删除失败";
        }
    }

    //修改药品的方法
    @ResponseBody
    @RequestMapping("editSdrugdictionary")
    public Object editSdrugdictionary(Drugdictionary drugdictionary){
        int result = drugdictionaryService.editSdrugdictionary(drugdictionary);
        if(result>0){
            return "修改成功";
        }else{
            return "修改失败";
        }

    }



}
